<?php

class GuanzhuModel extends DBModel
{
	protected $_table = '{{guanzhu}}';
	protected $_id = 'gid';
	
	/*
	public function getListByUserId($userid, $pageno, $pagesize)
	{
		$where['from'] = '{{guanzhu}} g';
		$where['select'] = 'g.userid,u.truename,u.sign,p.midpath logopath';
		$where['condition'] = 'g.userid = :userid';
		$where['join'] = 'left join {{user}} u on u.userid = g.userid 
						  left join {{photo}} p on u.logoid = p.photoid';
		$where['limit'] = $pagesize;
		$where['offset'] = max(0, $pageno - 1) * $pagesize;
		$params = array(':userid' => $userid);
		return $this->findAll($where, $params);
	}
	*/
	
	public function getListByUserIdAll($userid, $pageno, $pagesize)
	{
		$where['select'] = 'buserid,userid';
		$where['condition'] = 'userid = :userid or buserid = :userid';
		$where['limit'] = $pagesize;
		$where['offset'] = max(0, $pageno - 1) * $pagesize;
		//$where['group'] = 'buserid';
		$params = array(':userid' => $userid);
		return $this->findAll($where, $params);
	}
	
	public function getListByUserId($userid, $pageno, $pagesize)
	{
		$where['from'] = '{{guanzhu}} g';
		$where['select'] = 'g.buserid,u.nickname as truename,u.sign,p.midpath logopath';
		$where['condition'] = 'g.userid = :userid';
		$where['join'] = 'left join {{user}} u on u.userid = g.buserid
						  left join {{photo}} p on u.logoid = p.photoid';
		$where['limit'] = $pagesize;
		$where['offset'] = max(0, $pageno - 1) * $pagesize;
		$where['group'] = 'g.buserid';
		$params = array(':userid' => $userid);
		return $this->findAll($where, $params);
	}
	
	public function getBuserids($userid)
	{
		$where['select'] = 'group_concat(distinct buserid)';
		$where['condition'] = 'userid = :userid';
		$params = array(':userid' => $userid);
		return $this->findScalar($where, $params);
	}
	
	public function getListByBuserid($buserid, $pageno, $pagesize)
	{
		$where['from'] = '{{guanzhu}} g';
		$where['select'] = 'g.userid,g.createtime followtime,u.nickname as truename,u.sign,p.midpath logopath';
		$where['condition'] = 'g.buserid = :buserid';
		$where['join'] = 'left join {{user}} u on u.userid = g.userid 
						  left join {{photo}} p on u.logoid = p.photoid';
		$where['limit'] = $pagesize;
		$where['offset'] = max(0, $pageno - 1) * $pagesize;
		$params = array(':buserid' => $buserid);
		return $this->findAll($where, $params);
	}
	
	public function getListFans($buserid, $pageno, $pagesize)
	{
		$where['from'] = '{{guanzhu}} g';
		$where['select'] = 'g.userid,g.createtime followtime, sum(c.howmuch) howmuch';
		$where['condition'] = 'g.buserid = :buserid';
		$where['join'] = 'left join {{consume}} c on c.userid = g.userid and c.sorpuserid = g.buserid';
		$where['group'] = 'g.userid';
		$where['limit'] = $pagesize;
		$where['offset'] = max(0, $pageno - 1) * $pagesize;
		$where['order'] = 'howmuch desc';
		$params = array(':buserid' => $buserid);
		return $this->findAll($where, $params);
	}
	
	public function isExist($userid, $buserid)
	{
		$where['select'] = 'expiretime';
		$where['condition'] = 'userid = :userid and buserid = :buserid';
		$params = array(':userid' => $userid, ':buserid' => $buserid);
		$expiretime = $this->findScalar($where, $params);
		if ($expiretime && $expiretime > time()) {
			return '1';
		} else {
			return '0';
		}
	}
	
	public function isFans($userid)
	{
		$where['select'] = 'gid';
		$where['condition'] = 'userid = :userid and buserid != :buserid and expiretime > ' . time();
		$params = array(':userid' => $userid, ':buserid' => USERID);
		return $expiretime = $this->find($where, $params);
	}
	
	public function getUserCount($buserid)
	{
		$where['select'] = 'count(*)';
		$where['condition'] = 'buserid = :buserid';
		$params = array(':buserid' => $buserid);
		return $this->findScalar($where, $params);
	}
	
	public function getUserCountByUserID($userid)
	{
		$where['select'] = 'count(*)';
		$where['condition'] = 'userid = :userid';
		$params = array(':userid' => $userid);
		return $this->findScalar($where, $params);
	}
}